<template>
  <div class="">
    <div class="page-filter mb10 fl">
      <el-form ref="filter" :model="filter" label-width="0">
        <el-form-item class="mr10 mb5 fl">
          <el-select
            v-model="filter.pro_id"
            size="small"
            :remote-method="selPros"
            remote
            filterable
            style="width: 100%"
            placeholder="请选择项目名称"
            clearable
            @focus="selPros"
          >
            <el-option
              v-for="item in pors"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            />
          </el-select>
        </el-form-item>
        <el-form-item class="mr10 mb5 fl" style="width: 300px">
          <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
          <el-button type="primary" size="small" @click="handleAdd">新 增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table v-loading="app.loading" class="search-table" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="序号" align="center" width="56px">
        <template slot-scope="{$index}">
          {{ $index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="项目名称" min-width="220px" prop="pro_name" />
      <el-table-column label="PO单号" width="190px">
        <template slot-scope="{row}">
          {{'PO'+row.pro_id}}
        </template>
      </el-table-column>
      <el-table-column label="施工人员" width="110px" prop="nickname" />
      <el-table-column label="负责人" align="center" width="80px" :formatter="fdirector" />
      <el-table-column label="部门" width="150px" prop="dept_name" />
      <el-table-column label="职位" width="150px" prop="pos_name" />
      <el-table-column label="备注" min-width="260px" prop="remark" />
      <el-table-column label="操作" align="center" width="90px" fixed="right">
        <template slot-scope="{row}">
          <el-dropdown>
            <el-button type="primary" size="mini">
              操作<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <!-- <el-dropdown-item @click.native="handleEdit(row)">编 辑</el-dropdown-item> -->
              <el-dropdown-item @click.native="handleDelete(row)">删 除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    <Dialog ref="dialog" :get-data="getList" :info="dialog" />
  </div>
</template>

<script>
import { selPros, prostaffs, delProstaff } from '@/api/project'
import { getToken, setToken } from '@/utils/token/pro'
import Pagination from '@/components/Pagination'
import Dialog from './components/renyuan-dialog'

export default {
  name: '',
  components: { Pagination, Dialog },
  inject: ['app'],
  data() {
    return {
      list: [],
      pors: [],
      dialog: {
        edit: {},
        title: '',
        type: ''
      },
      filter: {
        pageNumber: 10,
        pageCurrent: 1,
        pageAll: 1
      }
    }
  },
  watch: {
    'filter.pro_id': {
      handler(n, o) {
        setToken(n ? n : '')
        this.getList()
      },
      immediate: false
    }
  },
  created() {
    this.selPros('', getToken())
    this.$set(this.filter, 'pro_id', getToken())
  },
  methods: {
    getList(e) {
      this.app.loading = true
      prostaffs({
        ...this.filter,
        ...e
      }).then(response => {
        this.list = response.list
        this.filter = response.filter
        this.app.removeLoding()
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    fdirector(e) {
      let s = '否'
      if(e.director === 1) s = '是'
      return s
    },
    selPros(query, id) {
      let str = ''
      if (Object.prototype.toString.call(query) === '[object String]') str = query
      selPros({ name: str, id: id ? id : '' }).then(response => {
        this.pors = response.pros
      }).catch(err => {
        this.$message.error(err)
      })
    },
    handleDelete(e) {
      this.$confirm('此操作将删除数据信息，数据将无法恢复, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        delProstaff({
          ids: e['id']
        }).then(response => {
          this.getList()
          this.$message.success('删除成功')
        })
      })
    },
    handleEdit(e) {
      this.dialog.title = '编辑人员'
      this.dialog.type = 'edit'
      this.app.dialogParam(this.dialog.edit, ['id', 'pro_id'], e)
      this.$refs['dialog'].visible = true
    },
    handleAdd(e) {
      this.dialog.title = '新增人员'
      this.dialog.type = 'add'
      e = {}
      this.app.dialogParam(this.dialog.edit, ['id', 'user', 'pro_id', 'remark', 'first'], e)
      this.$refs['dialog'].visible = true
    },
    outData(e) {

    }
  }
}
</script>
